import {WebSocketWrapper} from "../framework/WebsocketWrapper.ts";
import baseURL from "../api";
import {Stream} from "./Stream.ts";

export function dataEvent(cb:(event:any)=>{}): WebSocketWrapper {
    return webSocket(baseURL+"/ws/data", cb)
}
export function chartEvent(cb:(event:any)=>{}): WebSocketWrapper {
    return webSocket(baseURL+"/ws/chart", cb)
}
export function barchartEvent(cb:(event:any)=>{}): WebSocketWrapper {
    return webSocket(baseURL+"/ws/barchart", cb)
}
export function sse(cb:(event:any)=>{}): WebSocketWrapper {
    return webSocket(baseURL+"/ws/sse/sample", cb)
}

export function webSocket(path:string, cb:(event:any)=>{}): WebSocketWrapper {
    let ws = new WebSocketWrapper(path, cb)
    console.log("create webSocket", ws.url)
    return ws
}

export function createWs(): Stream {
    return ws(baseURL+'/ws/data')
}

/**
 * 创建websocket
 * @param path 相对路径,不支持跨域(base_url)
 */
export function createWebsocket(path: string): Stream {
    return ws(baseURL+path)
}

const ws = (path:string): Stream => {
    let ws = new WebSocketWrapper(path)
    let stream = new Stream(ws)
    console.log("create webSocket", stream.ws.id)
    return stream
}
